import upload from "./upload.vue";
import uploadExcel from "./uploadExcel.vue";
import { createVNode, render } from "vue";

let ctx;

/**
 * 上传组件
 *  @param {Object}  updParams:{
 *    limit: 1, //最大上传文件数
 *    fileSize: undefined, //文件大小-KB
 *    dimension: [],  //尺寸[宽-最小，宽-最大，高-最小，高-最大]
 *    accept: "", //上传文件类型 如: ".xls,.xlsx"
 *    acceptTxt: "", //提示信息
 *    extensions: [], //文件后缀
 *    uploadUrl: "", //上传地址
 *    typeCode: "", //附件根文件
 *    filePath: "", //文件路径
 *    bussId: "", //业务id
 *    custParam: {}, //自定义参数
 *    showMsgBox: true, //显示消息
 *    listType: "text", //文件列表的类型
 *    TMCode: "", //下载模版
 *    title:"",//窗口名称
 *    dialogWidth: 400, 窗口宽度
 *    dialogHeight: 450, 窗口高度
 * }
 * @return {Promise} 返回参数说明
 * [{
 *    light 指示灯	Number、String	枚举：1.红灯 2.黄灯 3.绿灯 其它灰灯
 *    retCode 错误代码	string
 *    retMsg 错误消息	string
 *    bussId 业务id	String
 *    fileName 文件名	String
 *    filePath 文件路径	String
 * }]
 */
export function szdUpload(updParams) {
  return new Promise(resolve => {
    let resolveFlag = false;
    const div = document.createElement("div");
    const vNode = createVNode(upload, {
      updParams: updParams,
      onUploadResult: res => {
        resolve(res);
        resolveFlag = true;
      },
      onCloseDialog: () => {
        if (!resolveFlag) {
          resolve(false);
        }
        document.body.removeChild(div);
      }
    });
    vNode.appContext = ctx;
    document.body.appendChild(div);
    render(vNode, div);
  });
}

/**
 * 上传Excel组件
 *  @param {Object}  updParams:{
 *     acceptTxt: "", //提示信息
 *     TMCode: "", //下载模板
 *     fileName: "", //文件名
 *     title: "", //标题
 *     dialogWidth: 300, //窗口宽度
 *     dialogHeight: 300 //窗口高度
 * }
 *
 * @param {Array} fCats 表头参数
 * [{
 *  field:列字段,
 *  label:列名称,
 *  type:数据类型-可选["string", "number", "boolean", "date", "datetime", "time", "year", "month"],
 *  numDec:小数位位数-可选,
 *  required: 必输-可选,
 *  importSplit: 数据分割符-可选,
 *  children:[{同fCatCol参数}]  //多表头-可选
 * }]
 *
 * @return {Promise} 返回参数-导入的数据
 */
export function szdUploadExcel(updParams, fCats = []) {
  return new Promise(resolve => {
    let resolveFlag = false;
    const div = document.createElement("div");
    const vNode = createVNode(uploadExcel, {
      updParams: updParams,
      fCats: fCats,
      onUploadResult: res => {
        resolve(res);
        resolveFlag = true;
      },
      onCloseDialog: () => {
        if (!resolveFlag) {
          resolve(false);
        }
        document.body.removeChild(div);
      }
    });
    vNode.appContext = ctx;
    document.body.appendChild(div);
    render(vNode, div);
  });
}

/**
 * 设置上下文
 */
const setShUpload = function (app) {
  ctx = app._context;
};

export default setShUpload;
